<template>
  <div class="login_content">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="login_img">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        :rules="rules"
        class="login_form"
        :model="loginForm"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm('loginFormRef')"
            >提交</el-button
          >
          <el-button type="info" @click="resetForm('loginFormRef')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //登录表单的数据绑定对象
      loginForm: {
        username: "admin",
        password: "123456",
      },
      //这是表单验证规则对象
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          //   { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          //提交了
          const result = await this.$http.post("login", this.loginForm);
          const res = result.data;
          console.log(res);
          if (res.meta.status == 200) {
            //要先在element.js中引入message
            this.$message.success("登录成功！");
            //下面是登录成功后的两步处理
            //1.登录成功后的token放在SessionStorage中
            window.sessionStorage.setItem("token", res.data.token);
            // 2.成功后通过编程式导航跳转到后台主页，即 /home
            this.$router.push("/home");
          } else {
            return this.$message.error("登录失败");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>

.login_content {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.login_img {
  width: 150px;
  height: 150px;
  background-color: #fff;
  /* 这里还是用position来搞好些 */
  margin-left: 150px;
  margin-top: -75px;
  border-radius: 50%;
  box-shadow: 0px 0px 5px 2px #aaa;
  padding: 10px;
}
.login_img img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
.login_form {
  margin-top: 35px;
  padding: 0 20px;
}
.btns {
  float: right;
}
</style>
